﻿using System;
using System.Collections.Generic;
using System.Linq;
using UygulamaTakbisService.wsTakbisServis;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace UygulamaTakbisService
{
    public class BirimBilgiDao : IDisposable
    {
        TAKBISWebService takbisServis = new TAKBISWebService();

        public bool BirimBilgiKaydet(BirimBilgi birimBilgi)
        {
            bool sonuc;
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TakbisBilgiSorguLogConnection"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    con.Open();
                    SqlTransaction transaction = con.BeginTransaction("trans");
                    try
                    {
                        cmd.CommandText = @"INSERT INTO BirimBilgi with(updlock) (BirimBilgiID,BirimTip,Ad,KayitTarih) VALUES (@BirimBilgiID,@BirimTip,@Ad,GETDATE())";

                        if (birimBilgi.BirimBilgiID != null)
                            cmd.Parameters.Add("@BirimBilgiID", birimBilgi.BirimBilgiID);
                        else
                            cmd.Parameters.Add("@BirimBilgiID", DBNull.Value);

                        if (birimBilgi.BirimTip != null)
                            cmd.Parameters.Add("@BirimTip", birimBilgi.BirimTip);
                        else
                            cmd.Parameters.Add("@BirimTip", DBNull.Value);

                        if (birimBilgi.Ad != null)
                            cmd.Parameters.Add("@Ad", birimBilgi.Ad);
                        else
                            cmd.Parameters.Add("@Ad", DBNull.Value);

                        cmd.Connection = con;
                        cmd.Transaction = transaction;
                        cmd.ExecuteScalar();
                        transaction.Commit();
                        sonuc = true;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        sonuc = false;
                        using (ExceptionLogDao el = new ExceptionLogDao())
                        {
                            el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                        }
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                    return sonuc;
                }
            }
        }

        public bool DovizBilgiKayitliMi(short birimBilgiID)
        {
            bool sonuc = false;
            try
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TakbisBilgiSorguLogConnection"].ToString()))
                {
                    string sql = string.Format(@"SELECT * FROM BirimBilgi with(nolock) WHERE BirimBilgiID = {0} Order by KayitTarih Desc ", birimBilgiID);
                    using (SqlCommand cmd = new SqlCommand(sql))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            cmd.CommandTimeout = 60000;
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            if (dt.Rows.Count >= 1)
                                sonuc = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
            }
            return sonuc;
        }

        public void Dispose()
        {
        }
    }
}
